package admin

import (
	"context"
	"github.com/gogf/gf/v2/errors/gcode"
	"github.com/gogf/gf/v2/errors/gerror"
	"github.com/gogf/gf/v2/frame/g"
	apiAdmin "miaoyu/api/backend/admin"
	modelAdmin "miaoyu/internal/model/admin"
	"miaoyu/internal/service"
)

// 文件管理
type fileCtl struct {
}

var File = new(fileCtl)

func (a *fileCtl) Upload(ctx context.Context, req *apiAdmin.FileUploadDoReq) (res *apiAdmin.FileUploadDoRes, err error) {
	if req.File == nil {
		return nil, gerror.NewCode(gcode.CodeMissingParameter, "请选择需要上传的文件")
	}
	result, err := service.File().UploadImg(ctx, modelAdmin.FileUploadInput{
		File:       req.File,
		RandomName: true,
		Module:     "common",
	})
	if err != nil {
		return nil, err
	}
	commonJson := service.JsonResp().CommonResp(g.RequestFromCtx(ctx))

	commonJson.SetSuccessJsonExit("ok", result)
	return
}
func (a *fileCtl) DoImgUpload(ctx context.Context, req *apiAdmin.AttachmentUploadDoReq) (res *apiAdmin.AttachmentUploadDoRes, err error) {
	if req.File == nil {
		return nil, gerror.NewCode(gcode.CodeMissingParameter, "请选择需要上传的文件")
	}
	result, err := service.File().UploadImg(ctx, modelAdmin.FileUploadInput{
		File:       req.File,
		RandomName: true,
		Module:     "attachment",
	})
	if err != nil {
		return nil, err
	}
	commonJson := service.JsonResp().CommonResp(g.RequestFromCtx(ctx))

	commonJson.SetSuccessJsonExit("ok", result)
	return
}
func (a *fileCtl) DoFileUpload(ctx context.Context, req *apiAdmin.AttachmentUploadDoReq) (res *apiAdmin.AttachmentUploadDoRes, err error) {
	if req.File == nil {
		return nil, gerror.NewCode(gcode.CodeMissingParameter, "请选择需要上传的文件")
	}
	result, err := service.File().UploadFile(ctx, modelAdmin.FileUploadInput{
		File:       req.File,
		RandomName: true,
		Module:     "attachment",
	})
	if err != nil {
		return nil, err
	}
	commonJson := service.JsonResp().CommonResp(g.RequestFromCtx(ctx))

	commonJson.SetSuccessJsonExit("ok", result)
	return
}
